import {
  response,
  errorHandle,
  url
} from './response';

const getToken = () => {
  return wx.getStorageSync('auth');
}

export default {
  /**
   * 查询因子
   * @param {*} data 
   * @param {*} successCallback 
   */
  getFactor(data, successCallback) {
    wx.request({
      url: `${url}/factor_manage/list`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 搜索因子
   * @param {*} data 
   * @param {*} successCallback 
   */
  searchtFactor(data, successCallback) {
    wx.request({
      url: `${url}/factor_manage/search`,
      method: 'get',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 删除因子
   * @param {*} data 
   * @param {*} successCallback 
   */
  removeFactor(data, successCallback) {
    wx.request({
      url: `${url}/factor_manage/remove`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 查询因子详情
   * @param {*} data 
   * @param {*} successCallback 
   */
  factorManage(id, successCallback) {
    wx.request({
      url: `${url}/factor_manage/${id}`,
      method: 'get',
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 添加因子模板
   * @param {*} data 
   * @param {*} successCallback 
   */
  addFactor(data, successCallback) {
    wx.request({
      url: `${url}/factor_manage/add`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 更新因子模板
   * @param {*} data 
   * @param {*} successCallback 
   */
  updateFactor(data, successCallback) {
    wx.request({
      url: `${url}/factor_manage/update`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 启用因子模板
   * @param {*} data 
   * @param {*} successCallback 
   */
  setEnable(data, successCallback) {
    wx.request({
      url: `${url}/factor_manage/update/enabled`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  }
}